﻿using System;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace MvcShop.MVC.Validation
{
    public static class ValidationException
    {
        //验证正整数
        [DebuggerStepThrough]
        public static bool IsNaturalNumber(String strNumber)
        {
            Regex objNotNaturalPattern = new Regex("[^0-9]");
            Regex objNaturalPattern = new Regex("0*[1-9][0-9]*");

            return !objNotNaturalPattern.IsMatch(strNumber) &&
                            objNaturalPattern.IsMatch(strNumber);
        }

        //验证正整数和零
        [DebuggerStepThrough]
        public static bool IsWholeNumber(string strNumber)
        {
            Regex objNotWholePattern = new Regex("[^0-9]");

            return !objNotWholePattern.IsMatch(strNumber);
        }

        //验证正 负整数
        [DebuggerStepThrough]
        public static bool IsInteger(string strNumber)
        {
            Regex objNotIntPattern = new Regex("[^0-9-]");
            Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");

            return !objNotIntPattern.IsMatch(strNumber) &&
                            objIntPattern.IsMatch(strNumber);
        }

        //验证正 负整数 与 真数
        [DebuggerStepThrough]
        public static bool IsPositiveNumber(string strNumber)
        {
            Regex objNotPositivePattern = new Regex("[^0-9.]");
            Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");

            return !objNotPositivePattern.IsMatch(strNumber) &&
                          objPositivePattern.IsMatch(strNumber) &&
                          !objTwoDotPattern.IsMatch(strNumber);
        }

        //验证是否是有效的数字
        [DebuggerStepThrough]
        public static bool IsNumber(string strNumber)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

            return !objNotNumberPattern.IsMatch(strNumber) &&
                          !objTwoDotPattern.IsMatch(strNumber) &&
                          !objTwoMinusPattern.IsMatch(strNumber) &&
                          objNumberPattern.IsMatch(strNumber);
        }

        //验证是否英文字母 
        [DebuggerStepThrough]
        public static bool IsAlpha(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z]");

            return !objAlphaPattern.IsMatch(strToCheck);
        }

        //验证是否英文字母和数字 
        [DebuggerStepThrough]
        public static bool IsAlphaNumeric(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");

            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }    
    }
}
